#include "aerial\Probe.h"

#include <iostream>

using namespace std;

Probe::Probe(hgeResourceManager* hgeResMan, HGE* hge, NetIO* netIO, float _x, float _y)
	:FlyObject(hgeResMan, hge, netIO)
{
	rot = 0.0f;
	vx = 0.0f;
	vy = -1.0f;
	shieldStatus = 0;
	shield_radius = halo_radius = 0.0f;
	//shield_time = 0.0f;

	x = _x; 
	y = _y;
	spr = new hgeSprite(hgeResMan->GetTexture("tex.assualt"), 0, 0, 210.0f, 260.0f);
	spr->SetHotSpot(105.0f, 130.0f);
	spr->SetColor(0xffffffff);

	shield = new hgeSprite(hgeResMan->GetTexture("tex.particle"), 96, 64, 32, 32);
	shield->SetHotSpot(16.0f, 16.0f);
	shield->SetColor(0x500000ff);

	shieldHalo = new hgeSprite(hgeResMan->GetTexture("tex.particle"), 96, 64, 32, 32);
	shieldHalo->SetHotSpot(16.0f, 16.0f);
	shieldHalo->SetColor(0xa0ffffff);
}

Probe::~Probe() { delete spr; }	// FIXME: delete shieldHalo?

void Probe::render()
{
	spr->RenderEx(x, y, rot, 0.2f, 0.2f);
	if (shield_radius) {
		shield->RenderEx(x, y, shield_radius, shield_radius);
		shieldHalo->RenderEx(x, y, halo_radius, halo_radius);
	}
}

bool Probe::update(float dt)
{
	if (shieldStatus == 2) 
	{
		shield_radius += 0.2f;
		if (shield_radius > 3.5f)
		{
			shield_radius = 3.5f;
			shieldStatus = 3;
		}
	}
	else if (shieldStatus == 1)
	{
		shield_radius -= 0.2f;
		if (shield_radius < 0)
		{
			shield_radius = 0.0f;
			shieldStatus = 0;
		}
	}

	if (shieldStatus) 
	{
		//shield_time += dt;

		if (R < 255) ++R;
		if (B != 0) --B;

		DWORD shield_color = (0x50 << 24) + (R << 16) + B;
		shield->SetColor(shield_color);

		halo_radius += 0.1f;
		if (halo_radius > shield_radius + 0.5) halo_radius = 0.0f;
	} else {
		//shield_time = 0.0f;
		R = 0;
		B = 255;
	}
	return false;
}

void Probe::clockwiseRotate(float dt)
{
	rot += 2.0f * dt;
	if (rot > 6.283) rot -= 6.283f;
	hgeVector a(0.f, -1.f);
	a.Rotate(rot);
	vx = a.x;
	vy = a.y;
}

void Probe::cclockwiseRotate(float dt)
{
	rot -= 2.0f * dt;
	if (rot < 0) rot += 6.283f;
	hgeVector a(0.f, -1.f);
	a.Rotate(rot);
	vx = a.x;
	vy = a.y;
}

